package com.ycwl.freetv.program;

import android.content.Context;
import android.graphics.Color;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.Glide;
import com.ycwl.library.ParseUIFromJson;
import com.ycwl.freetv.program.Image;

import java.io.File;
import java.util.ArrayList;

public class Slide implements ProgramInterface {
    static String TAG = "Slide";
    final ViewPager2 viewPager2;
    final Context context;
    final ParseUIFromJson.BaseNode baseNode;

    final Handler handler;
    final Runnable runnable;
    protected int pageIndex;
    final class BannerAdapter extends RecyclerView.Adapter {
        Context context;
        ParseUIFromJson.BaseNode[] dataArrayList;
        public BannerAdapter(Context context,ParseUIFromJson.BaseNode[] dataArrayList) {
            super();
            this.context = context;
            this.dataArrayList = dataArrayList;
        }
        class BannerHolder extends RecyclerView.ViewHolder {
            public BannerHolder(@NonNull View itemView) {
                super(itemView);
            }
        }
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            RelativeLayout relativeLayout = new RelativeLayout(this.context);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
            relativeLayout.setLayoutParams(layoutParams);
            relativeLayout.setBackgroundColor(Color.WHITE);
            return new BannerHolder(relativeLayout);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ParseUIFromJson.BaseNode  baseNode = this.dataArrayList[position];
            View view = null;
            if (baseNode.type == ParseUIFromJson.NODE_TYPE.IMAGE) {
                ImageView imageView = new ImageView(context);
                ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(10, 10);
                marginLayoutParams.leftMargin = baseNode.x.intValue();
                marginLayoutParams.topMargin = baseNode.y.intValue();
                marginLayoutParams.width = baseNode.width.intValue();
                marginLayoutParams.height = baseNode.height.intValue();
                imageView.setLayoutParams(marginLayoutParams);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);

                File file = new File(baseNode.url); // 替换为你的图片文件路径
                Glide.with(context)
                        .load(baseNode.url)
                        .into(imageView);
                view = imageView;
            } else if (baseNode.type == ParseUIFromJson.NODE_TYPE.TEXT) {
                TextView textView = new TextView(this.context);
                RelativeLayout.LayoutParams layoutParams = new  RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
                textView.setLayoutParams(layoutParams);

                try {
                    if (baseNode.text_size != null) {
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, baseNode.text_size.intValue());
                    } else {
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 50);
                    }
                    if  (baseNode.text_background_color != null && baseNode.text_background_color.length() > 1 && baseNode.text_background_color.startsWith("#")) {
                        textView.setBackgroundColor(Color.parseColor(baseNode.text_background_color));
                    }
                    if  (baseNode.text_color != null && baseNode.text_color.length() > 1 && baseNode.text_color.startsWith("#")) {
                        textView.setTextColor(Color.parseColor(baseNode.text_color));
                    }
                } catch (NumberFormatException ignored) {
                    Toast.makeText(context.getApplicationContext(), "文字设置颜色失效" + baseNode.name, Toast.LENGTH_LONG).show();
                }

                textView.setText(baseNode.content);
                view = textView;
            }

            if (view == null) {
                return;
            }
            if (holder.itemView instanceof RelativeLayout) {
                RelativeLayout relativeLayout = (RelativeLayout)holder.itemView;
                if  (baseNode.text_background_color != null && baseNode.text_background_color.length() > 1 && baseNode.text_background_color.startsWith("#")) {
                    relativeLayout.setBackgroundColor(Color.parseColor(baseNode.text_background_color));
                }
                relativeLayout.addView(view);
            } else {
                Log.e(TAG, "itemView not RelativeLayout");
            }
        }

        @Override
        public int getItemCount() {
            return dataArrayList.length;
        }
    }

    public Slide(Context context, ParseUIFromJson.BaseNode baseNode) {
        this.context = context;
        this.baseNode = baseNode;
        this.pageIndex = 0;
        ViewPager2 viewPager21 = new ViewPager2(this.context);
        ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(960, 540);
        marginLayoutParams.leftMargin = baseNode.x.intValue();
        marginLayoutParams.topMargin = baseNode.y.intValue();
        marginLayoutParams.width = baseNode.width.intValue();
        marginLayoutParams.height = baseNode.height.intValue();
        viewPager21.setLayoutParams(marginLayoutParams);
        this.viewPager2 = viewPager21;
        BannerAdapter bannerAdapter = new BannerAdapter(context, baseNode.children);
        this.viewPager2.setAdapter(bannerAdapter);
        this.handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 1:
                        viewPager2.setCurrentItem(msg.arg1);
                        break;
                    default:
                        break;
                }
            }
        };
        this.runnable = new Runnable() {
            @Override
            public void run() {
                if (pageIndex + 1 >= baseNode.children.length) {
                    pageIndex = 0;
                } else {
                    pageIndex += 1;
                }
                Message message = new Message();
                message.what = 1;
                message.arg1 = pageIndex;

                handler.sendMessage(message);
                if (baseNode.children[pageIndex].total_time.intValue() > 0) {
                    handler.postDelayed(this, baseNode.children[pageIndex].total_time.intValue() * 1000);
                }

            }
        };
    }
    @Override
    public void play() {
        Log.d("slid", "total_time:" + baseNode.children[pageIndex].total_time.intValue());
        if (pageIndex < baseNode.children.length && baseNode.children[pageIndex].total_time.intValue() > 0) {
            handler.postDelayed(this.runnable, baseNode.children[pageIndex].total_time.intValue() * 1000);
        }
    }

    @Override
    public void stop() {
        this.handler.removeCallbacks(this.runnable);
    }

    @Override
    public void setTotalTime(int totalTime) {

    }

    @Override
    public void pause() {
        this.handler.removeCallbacks(this.runnable);
    }

    @Override
    public View getView() {
        return this.viewPager2;
    }
}
